/*
 * PositionEditingPanel.java
 *
 * Created on 26 wrzesień 2008, 11:38
 */
package ania2.gui;

import ania2.BoardPosition;
import ania2.SetupPositionBoardListener;
import ania2.enums.FieldStatesEnum;
import java.awt.Dimension;
import javax.swing.JRadioButton;

/**
 *
 * @author  pawel
 */
public class PositionSetupPanel extends javax.swing.JPanel {

    private SetupPositionBoardListener boardListener;

    /** Creates new form PositionEditingPanel */
    public PositionSetupPanel() {
        initComponents();
        board1.setBoardDescriptionPrinted(false);
        board1.getPosition().setStandardBeginingPosition();
        boardListener = new SetupPositionBoardListener(board1.getPosition(), board1.getBorderWidth(), board1.getWidth());
        board1.setBorderWidth(8);
        board1.addMouseListener(boardListener);
        boardListener.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals(boardListener.PROP_STATE)) {
                    int state = boardListener.getState();
                    FieldStatesEnum s = FieldStatesEnum.values()[state];
                    switch (s) {
                        case EMPTY:
                            jRadioButton1.setSelected(true);
                            break;
                        case WHITE_PIECE:
                            jRadioButton2.setSelected(true);
                            break;
                        case WHITE_QUEEN:
                            jRadioButton3.setSelected(true);
                            break;
                        case BLACK_PIECE:
                            jRadioButton4.setSelected(true);
                            break;
                        case BLACK_QUEEN:
                            jRadioButton5.setSelected(true);
                            break;
                    }
                } else {
                    board1.repaint();
                }
            }
        });
        java.awt.event.ItemListener itemListener = new java.awt.event.ItemListener() {

            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jRadioButtonItemStateChanged(evt);
            }
        };
        jRadioButton1.addItemListener(itemListener);
        jRadioButton2.addItemListener(itemListener);
        jRadioButton3.addItemListener(itemListener);
        jRadioButton4.addItemListener(itemListener);
        jRadioButton5.addItemListener(itemListener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();
        jRadioButton5 = new javax.swing.JRadioButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        board1 = new ania2.draughtsboard.Board();

        setName("Form"); // NOI18N

        buttonGroup1.add(jRadioButton1);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ania2.Ania2App.class).getContext().getResourceMap(PositionSetupPanel.class);
        jRadioButton1.setText(resourceMap.getString("jRadioButton1.text")); // NOI18N
        jRadioButton1.setName("jRadioButton1"); // NOI18N

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText(resourceMap.getString("jRadioButton2.text")); // NOI18N
        jRadioButton2.setName("jRadioButton2"); // NOI18N

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText(resourceMap.getString("jRadioButton3.text")); // NOI18N
        jRadioButton3.setName("jRadioButton3"); // NOI18N

        buttonGroup1.add(jRadioButton4);
        jRadioButton4.setText(resourceMap.getString("jRadioButton4.text")); // NOI18N
        jRadioButton4.setName("jRadioButton4"); // NOI18N

        buttonGroup1.add(jRadioButton5);
        jRadioButton5.setText(resourceMap.getString("jRadioButton5.text")); // NOI18N
        jRadioButton5.setName("jRadioButton5"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        board1.setName("board1"); // NOI18N
        board1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                board1PropertyChange(evt);
            }
        });

        javax.swing.GroupLayout board1Layout = new javax.swing.GroupLayout(board1);
        board1.setLayout(board1Layout);
        board1Layout.setHorizontalGroup(
            board1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 310, Short.MAX_VALUE)
        );
        board1Layout.setVerticalGroup(
            board1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 314, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(board1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jRadioButton1)
                            .addComponent(jRadioButton2)
                            .addComponent(jRadioButton3)
                            .addComponent(jRadioButton4)
                            .addComponent(jRadioButton5)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton1)
                        .addComponent(jButton2)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2, jRadioButton1, jRadioButton2, jRadioButton3, jRadioButton4, jRadioButton5});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(board1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jRadioButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jRadioButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void board1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_board1PropertyChange
// TODO add your handling code here:
    String property = evt.getPropertyName();
    if (property.equals("size")) {
        if (evt.getNewValue() instanceof Dimension) {
            Dimension d = (Dimension) evt.getNewValue();
            boardListener.updateBoardSize(d.width);
        }
    } else if (property.equals("borderWidth")) {
        if (evt.getNewValue() instanceof Integer) {
            int newValue = (Integer) evt.getNewValue();
            boardListener.setBorder(newValue);
        }
    }
}//GEN-LAST:event_board1PropertyChange

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
    board1.getPosition().setStandardBeginingPosition();
    board1.repaint();
}//GEN-LAST:event_jButton1MouseClicked

private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
// TODO add your handling code here:
    BoardPosition position = board1.getPosition();
    position.setBlackPieces(0);
    position.setWhitePieces(0);
    position.setQueens(0);
    board1.repaint();
}//GEN-LAST:event_jButton2MouseClicked

private void jRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {
        String source = ((JRadioButton) evt.getSource()).getName();
        char id = source.charAt(source.length() - 1);

        switch (id) {
            case '1':
                boardListener.setState(FieldStatesEnum.EMPTY.ordinal());
                break;
            case '2':
                boardListener.setState(FieldStatesEnum.WHITE_PIECE.ordinal());
                break;
            case '3':
                boardListener.setState(FieldStatesEnum.WHITE_QUEEN.ordinal());
                break;
            case '4':
                boardListener.setState(FieldStatesEnum.BLACK_PIECE.ordinal());
                break;
            case '5':
                boardListener.setState(FieldStatesEnum.BLACK_QUEEN.ordinal());
                break;
        }
    }

    public void setPosition(BoardPosition position) {
        board1.setPosition(position);
        boardListener.setPosition(position);
    }

    public BoardPosition getPosition() {
        board1.getPosition().computeHashAndLockCode();
        return board1.getPosition();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private ania2.draughtsboard.Board board1;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    private javax.swing.JRadioButton jRadioButton5;
    // End of variables declaration//GEN-END:variables
}
